package com.example.qqsave;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText account, pwd;
    Button btn_login, btn_reg, btn_man;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        account = findViewById(R.id.editText);
        pwd = findViewById(R.id.editText2);
        btn_login = findViewById(R.id.button);
        btn_reg = findViewById(R.id.button2);
        btn_man = findViewById(R.id.button3);
        btn_man.setOnClickListener(this);
        btn_reg.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                /**
                 * 登陆判断
                 * 1.首先获取当前保存的账号和密码信息
                 * 2.获取当前界面输入的账号信息和密码
                 * 3.判断提示
                 */
                //1
                SharedPreferences sp = this.getSharedPreferences("data", Context.MODE_PRIVATE);
                String accountStr = sp.getString("userName", "");
                String pwdStr = sp.getString("pwd", "");
                //2
                String account_input = account.getText().toString();
                String pwd_input = pwd.getText().toString();
                //3
                if (accountStr.isEmpty() || pwdStr.isEmpty()) {
                    Toast.makeText(this, "清闲注册", Toast.LENGTH_LONG).show();
                } else {
                    if (account_input.equals(accountStr) && pwdStr.equals(pwd_input)) {
                        Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(this, "密码账号不匹配", Toast.LENGTH_LONG).show();
                    }
                }
                break;
            case R.id.button2:
                Intent intent = new Intent(MainActivity.this, Register.class);
                startActivity(intent);
                break;
            case R.id.button3:
                Intent intent_3 = new Intent(MainActivity.this, ManageActivity.class);
                startActivity(intent_3);
                break;
        }
    }
}
